package com.example.myerp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.myerp.S_gather_Dto;
import com.example.myerp.mapper.S_gather_detailsMapper;
import com.example.myerp.pojo.Inbound_Scheduling;
import com.example.myerp.pojo.S_gather;
import com.example.myerp.mapper.S_gatherMapper;
import com.example.myerp.pojo.S_gather_details;
import com.example.myerp.service.QualityService;
import com.example.myerp.service.S_gatherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mycloud.api.pojo.ExeorderDt;
import com.mycloud.api.pojo.Quality_control;
import com.mycloud.api.pojo.Quality_controlDt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhuxingyu
 * @since 2021-08-10
 */
@Service
@SuppressWarnings("all")
public class S_gatherServiceImpl extends ServiceImpl<S_gatherMapper, S_gather> implements S_gatherService {

    @Autowired
    S_gatherMapper s_gatherMapper;
    @Autowired
    S_gather_detailsMapper S_gather_detailsMapper;

    @Autowired
    QualityService service;

    @Override
    public void insertS_gather(S_gather_Dto s_gather_dto) {
        S_gather s_gather = s_gather_dto.getS_gather();
        s_gather.setGatherId(UUID.randomUUID() + "");
        s_gatherMapper.insertS_gather(s_gather);
        List<S_gather_details> s_gather_details = s_gather_dto.getS_gather_details();
        s_gather_details.forEach(item -> {
            item.setParentId(s_gather.getId());
            S_gather_detailsMapper.insertS_gather_details(item);
            //入库调度
        });

    }

    @Override
    public void insertS_gather2(S_gather_Dto s_gather_dto) {
        S_gather s_gather = s_gather_dto.getS_gather();
        s_gatherMapper.insertS_gather2(s_gather);
        List<S_gather_details> s_gather_details = s_gather_dto.getS_gather_details();
        s_gather_details.forEach(item -> {
            item.setParentId(s_gather.getId());
            S_gather_detailsMapper.insertS_gather_details2(item);
        });
    }

    @Override
    public IPage<S_gather> regSgatherScell(Long pageNum, Long pageSize) {
        Page<S_gather> page = new Page<>(pageNum, pageSize, true);
        return s_gatherMapper.regSgatherScell(page);
    }

    @Override
    public IPage<S_gather> sgatherRegChckList(Long pageNum, Long pageSize) {
        Page<S_gather> page = new Page<>(pageNum, pageSize, true);
        return s_gatherMapper.sgatherRegChckList(page);
    }

    @Override
    public IPage<S_gather> chenkSgaRegCell(Long pageNum, Long pageSize) {
        Page<S_gather> page = new Page<>(pageNum, pageSize, true);
        return s_gatherMapper.chenkSgaRegCell(page);
    }

    @Override
    public IPage<S_gather> sPayCellInfoDet(Long pageNum, Long pageSize) {
        Page<S_gather> page = new Page<>(pageNum, pageSize, true);
        return s_gatherMapper.sPayCellInfoDet(page);
    }

    @Override
    public void checkRegSgather(S_gather s_gather) {
        s_gatherMapper.checkRegSgather(s_gather);
    }

    @Override
    public void noCheckRegSgather(Long id) {
        s_gatherMapper.noCheckRegSgather(id);
    }

    @Override
    public IPage<S_gather> makeSgatherStore(Long pageNum, Long pageSize) {
        Page<S_gather> page = new Page<>(pageNum, pageSize, true);
        return s_gatherMapper.makeSgatherStore(page);
    }

    @Override
    public Integer updInbSch(Inbound_Scheduling ins) {
        S_gather_detailsMapper.updInbSch(ins);
        Long id = ins.getParentId();
        Integer integer = S_gather_detailsMapper.selInbSchCount(id);
        Integer integer1 = S_gather_detailsMapper.selInbSchCount_p(id);
        Integer row = 0;
        if (integer == integer1) {
            s_gatherMapper.updInbSch(id);
            row = 1;
        }

        return row; //0:继续调度 ; 调度完成
    }

    @Override
    public void updStatus(S_gather_Dto sGatherDto) {
        List<Inbound_Scheduling> ins = sGatherDto.getIns();
        Long parentId = null;
        Integer sum = 0;
        Long id = null;
        for (int i = 0; i < ins.size(); i++) {
            S_gather_detailsMapper.updasthdea(ins.get(i).getNum(), ins.get(i).getId());
            sum = sum + ins.get(i).getNum();
            id = ins.get(i).getParentId();
//            s_gatherMapper.updCellAmount(ins.get(i).getNum(), ins.get(i).getProductId());
        }
        s_gatherMapper.updSgatatus(sum, id);

    }

    @Override
    public void updaGthOver(S_gather_Dto sGatherDto) {
        List<Inbound_Scheduling> ins = sGatherDto.getIns();
        S_gather s_gather = sGatherDto.getS_gather();
        Integer sum = 0;
        for (int i = 0; i < ins.size(); i++) {
            S_gather_detailsMapper.updasthOver(ins.get(i).getGatheredAmount(), ins.get(i).getId());
            sum = sum + ins.get(i).getGatheredAmount();

            if (s_gather.getReasonexact().equals("采购放库")) {
                Integer productId = ins.get(i).getProductId();
                Long orderId = ins.get(i).getOrderId();
                String productName = ins.get(i).getProductName();
                Integer amount = ins.get(i).getAmount();
                Integer integer = service.selConRow(orderId, productId);
                if (integer == 0) {
                    service.updaCargo(orderId, productId);
                    Quality_control quality_control = new Quality_control(orderId, productId, productName, amount, amount, amount);
                    service.addQc(quality_control);
                    Long exeorderdtId = ins.get(i).getExeorderdtId();
                    List<ExeorderDt> exeorderDt = service.selExeorder(orderId, productId, exeorderdtId);
                    ExeorderDt exeorderDt1 = exeorderDt.get(0);
                    Long fileId = exeorderDt1.getFileId();
                    String contact = exeorderDt1.getContact();
                    String supplierName = exeorderDt1.getSupplierName();
                    String iphone = exeorderDt1.getIphone();
                    Long id = quality_control.getId();
                    Long maxId = service.selMaxId(orderId, productId);
                    Quality_controlDt dt = new Quality_controlDt(maxId, fileId, supplierName, amount, 0, 0, contact, iphone, productId, orderId, exeorderdtId);
                    service.addQcDt(dt);
                }
            } else {
                s_gatherMapper.updCellAmount(ins.get(i).getGatheredAmount(), ins.get(i).getProductId());
            }
        }
        s_gather.setGatheredAmountSum(sum);
        s_gatherMapper.updaGthOver(s_gather);
    }




}
